GCC Survival Guide
GCC is the Gnu Compiler Collection. Additional information about it can be found at the official site. It is the front end for many different compilers, including c and c++. Using GCC Having a compiler is nice, but when all that compiler offers is a bunch of command line tools new people can get a bit discouraged. Integrated development environments usually offer a way to do the compiler-calling for you, but still, it is useful to know what is going on there. Compiling C programs GCC in its totality is a huge beast, with hundreds of different command line switches. Fortunately, in everyday life you'll only need a small subset of those. The basic way to call GCC is: gcc program.c -o program This compiles 'program.c' into the executable file 'program' ('program.exe' on Windows). If no output (-o) file is specified the executable will be 'a.out'. Compiling C++ programs To compile C++ programs you must use the 'g++' command instead of 'gcc'. On some systems the command is 'gxx'. G++ takes mostly the same arguments as GCC. g++ program.cpp -o program or gxx program.cpp -o program Introduction to libraries The next thing that comes up next is libraries, you need to link certain libraries with your program. Say we need OpenGL and X11 libraries: gcc program.c -o program -lGL -lX11 The names used after the '-l' arguments will cause GCC to look for files with names like libGL.a and libX11.a in its default library path. This path can be extended with the -L option, and similarly the path that is searched for files included by the code can be extended with -I (capital i). gcc program.c -o program -lGL -lX11 -L/usr/local/my-gl/lib -I/usr/local/my-gl/include Building multiple source files into a single executable can be done by just adding more files where it currently says 'program.c'. Compiling this way requires everything to be compiled every time though, which is rather slow. Source files can also be compiled into 'object' files, and later a bunch of object files can be linked together to produce an executable. This is what the '-c' option is for. gcc -c program.c -o program.o gcc -c utility.c -o utility.o gcc utility.o program.o -lGL -lX11 Note that the libraries only need to be passed when linking objects together. Optimization basics A few switches can come in handy when you are trying to compile lean mean fast code. Firstly, you'll want to turn on optimizations (note that this does slow down compiling significantly). This is done with the '-O' switch, after '-O' comes a number indicating how much effort the compiler has to spend on optimizing. '-O2' is usually good. When linking you can pass the '-s' option, which causes the compiler to remove everything that is unnecessary from the executable. This can seriously trim down executable size. gcc -O2 -c program.c -o program.o gcc -O2 -c utility.c -o utility.o gcc utility.o program.o -s -lGL -lX11 Next steps Calling the compiler manually every time you want to compile something is awfully cumbersome, which is why makefiles were invented. Those are basically a rather clever kind of scripts that automatically recompile things when needed. Writing them is a whole other subject though. (See the makefile manual for more about make and makefiles.)